package com.firend.controller;

import com.order.hit.crossover.platform.common.bean.User;
import com.order.hit.crossover.platform.common.dao.OrderDao;
import com.order.hit.crossover.platform.common.utils.PFResponse;
import com.order.hit.crossover.platform.common.bean.Friend;
import com.order.hit.crossover.platform.common.bean.Notice;
import com.firend.mapper.FriendMapper;
import com.order.hit.crossover.platform.common.utils.TimeChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lei
 * @version 0.1
 * @date 2020/9/8
 */
@RestController
@RequestMapping("/friend")
public class FriendControler {
    @Autowired
    private FriendMapper friendMapper;

    // 添加朋友关系
    @PostMapping("/addfriend")
    public PFResponse addFriend(@RequestBody Friend friend){
        PFResponse response = new PFResponse();
        Friend f = this.friendMapper.getFriend(friend.getUserId(), friend.getFriendId());
        if (f != null && f.getFriendState().equalsIgnoreCase("FRIEND")){
            return response.code(200).message("你已经添加了这个朋友");
        }
        if (f != null && f.getFriendState().equalsIgnoreCase("WAIT")){
            return response.code(200).message("正在等待此 用户验证朋友关系");
        }
        try {
            friend.setFriendState("WAIT");
            this.friendMapper.addfriend(friend);
            response.code(200);
            response.message("添加成功，等待用户验证");

            //  为收到好友申请的用户生成通知
            User user = (User)new RestTemplate().exchange(
                            "http://system:18080/user/getuserByid/"+ friend.getUserId(),
                            HttpMethod.GET,
                            new HttpEntity<>(new HttpHeaders()),
                            PFResponse.class).getBody().get("data");
            Notice notice = new Notice("您已收到" + user.getUserName() + "的好友请求，请到好友申请界面查看详细信息！", 0, 1, friend.getFriendId()+"");
            new RestTemplate().exchange("http://notice:18083/Notice/add", HttpMethod.POST, new HttpEntity<>(notice,new HttpHeaders()), PFResponse.class);
        }catch (Exception e){
            response.code(500);
            response.message("添加失败");
        }
        return response;
    }

    // 确定朋友关系
    @PostMapping("/surefriend")
    public PFResponse sureFriend(@RequestBody Friend friend){
        PFResponse response = new PFResponse();
        Friend f = this.friendMapper.getFriend(friend.getUserId(), friend.getFriendId());
        if (f != null && f.getFriendState().equalsIgnoreCase("FRIEND")){
            return response.code(200).message("你已经添加了这个朋友");
        }
        try{
            friend.setFriendState("FRIEND");
            this.friendMapper.changeState(friend);
            Friend friend1 = new Friend();
            friend1.setUserId(friend.getFriendId());
            friend1.setFriendId(friend.getUserId());
            friend1.setFriendState(friend.getFriendState());
            this.friendMapper.addfriend(friend1);
            response.code(200);
            response.message("确定朋友关系");

            //  为发起好友申请的用户生成通知
            User user = (User)new RestTemplate().exchange(
                    "http://system:18080/user/getuserByid/"+ friend.getFriendId(),
                    HttpMethod.GET,
                    new HttpEntity<>(new HttpHeaders()),
                    PFResponse.class).getBody().get("data");
            Notice notice = new Notice("您向" + user.getUserName() + "发送的好友请求已通过，请到我的好友界面查看详细信息！", 0, 1, friend.getUserId()+ "");
            new RestTemplate().exchange("http://notice:18083/Notice/add", HttpMethod.POST, new HttpEntity<>(notice,new HttpHeaders()), PFResponse.class);
//            noticeService.add(notice);

        }catch (Exception e){
            response.code(500);
            response.message("确认失败");
        }
        return response;
    }

    // 拒绝好友请求
    @PostMapping("/refusefriend")
    public PFResponse refuseFriend(@RequestBody Friend friend){
        PFResponse response = new PFResponse();
        try{
            this.friendMapper.deletefriend(friend);
            response.code(200);
            response.message("拒绝成功");

            //  为发起好友申请的用户生成通知
//            User user = userMapper.getUserByid(friend.getFriendId());
            User user = (User)new RestTemplate().exchange(
                    "http://system:18080/user/getuserByid/"+ friend.getFriendId(),
                    HttpMethod.GET,
                    new HttpEntity<>(new HttpHeaders()),
                    PFResponse.class).getBody().get("data");
            Notice notice = new Notice("您向" + user.getUserName() + "发送的好友请求已被拒绝！", 0, 1, friend.getUserId()+ "");
            new RestTemplate().exchange("http://notice:18083/Notice/add", HttpMethod.POST, new HttpEntity<>(notice,new HttpHeaders()), PFResponse.class);
//            noticeService.add(notice);

        }catch (Exception e){
            response.code(200);
            response.message("拒绝失败");
        }
        return response;
    }


    // 删除朋友关系,双向删除
    @DeleteMapping("/deletefriend")
    public PFResponse deleteFriend(@RequestBody Friend friend){
        PFResponse response = new PFResponse();
        try {
            this.friendMapper.deletefriend(friend);
            Friend friend1 = new Friend();
            friend1.setUserId(friend.getFriendId());
            friend1.setFriendId(friend.getUserId());
            this.friendMapper.deletefriend(friend1);
            response.code(200);
            response.message("删除成功");

            //  为双方生成通知
//            User user1 = userMapper.getUserByid(friend.getUserId());
            User user1 = (User)new RestTemplate().exchange(
                    "http://system:18080/user/getuserByid/"+ friend.getUserId(),
                    HttpMethod.GET,
                    new HttpEntity<>(new HttpHeaders()),
                    PFResponse.class).getBody().get("data");
            User user2 = (User)new RestTemplate().exchange(
                    "http://system:18080/user/getuserByid/"+ friend.getFriendId(),
                    HttpMethod.GET,
                    new HttpEntity<>(new HttpHeaders()),
                    PFResponse.class).getBody().get("data");
//            User user2 = userMapper.getUserByid(friend.getFriendId());
            Notice notice1 = new Notice("您的好友" + user1.getUserName() + "发送的好友请求已通过，请到我的好友界面查看详细信息！", 0, 1, user2.getUserId() + "");
            Notice notice2 = new Notice("您的好友" + user2.getUserName() + "发送的好友请求已通过，请到我的好友界面查看详细信息！", 0, 1, user1.getUserId() + "");

//            noticeService.add(notice1);
            new RestTemplate().exchange("http://notice:18083/Notice/add", HttpMethod.POST, new HttpEntity<>(notice1,new HttpHeaders()), PFResponse.class);
            new RestTemplate().exchange("http://notice:18083/Notice/add", HttpMethod.POST, new HttpEntity<>(notice1,new HttpHeaders()), PFResponse.class);
//            noticeService.add(notice2);

        }catch (Exception e){
            response.code(500);
            response.message("删除失败");
        }
        return response;
    }

    // 查看好友列表 已经成功的
    @GetMapping("/getFriendByid/{id}")
    public PFResponse getFriend(@PathVariable("id")int id){
        List<Friend> list = this.friendMapper.getUserFriendByState(id, "FRIEND");
        if (list.isEmpty() || list == null){
            list = new ArrayList<>();
        }
        return new PFResponse().code(200).message("查询成功").data(list);
    }

    // id 作为主用户向其他人请求好友， state 为 WAIT
    @GetMapping("/getRequestFriend/{id}")
    public PFResponse getRequestFriend(@PathVariable("id")int id){
        List<Friend> list = this.friendMapper.getUserFriendByState(id, "WAIT");
        if(list.isEmpty() || list==null){
            list = new ArrayList<>();
        }
        return new PFResponse().code(200).message("查询成功").data(list);
    }

    // id 作为主朋友id，表示其他人向自己请求好友进行接收或者拒绝， state 为 WAIT
    @GetMapping("/getApplyFriend/{id}")
    public PFResponse getApplyFriend(@PathVariable("id")int id){
        List<Friend> list = this.friendMapper.getUserApplyByState(id, "WAIT");
        if(list.isEmpty() || list==null){
            list = new ArrayList<>();
        }
        return new PFResponse().code(200).message("查询成功").data(list);
    }

}